//
//  MyPresentationController.swift
//  todaySwift
//
//  Created by ChokWah on 2018/4/10.
//  Copyright © 2018年 ChokWah. All rights reserved.
//

import UIKit

// iOS8之后，新出的弹窗控制器
class MyPresentationController: UIPresentationController {
    var presentFrame: CGRect?
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        NotificationCenter.default.addObserver(self, selector: #selector(dismissPresentViewController), name: NSNotification.Name(rawValue: MyPresentationControllerDismiss), object: nil)
        
    }
    
    // 即将布局转场，子视图时调用
    override func containerViewWillLayoutSubviews() {
        presentedView?.frame = presentFrame!
        let coverView = UIView(frame: UIScreen.main.bounds)
        coverView.backgroundColor = .clear
        let tap = UITapGestureRecognizer(target: self , action: #selector(dismissPresentViewController))
        coverView.addGestureRecognizer(tap)
        // 在容器上添加一个蒙版
        containerView?.insertSubview(coverView, at: 0)
    }
    
    // 移除弹出的控制器
    @objc func dismissPresentViewController() {
        presentedViewController.dismiss(animated: false, completion: nil)
    }
}

